﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

namespace SynoManager.GUI
{
    public partial class ExceptionForm : Form
    {
        private readonly Exception ex;

        public ExceptionForm()
        {
            InitializeComponent();
        }

        public ExceptionForm(Exception ex)
            :this()
        {
            this.ex = ex;
            this.Size = new Size(393, 228);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Date and Time:     " + DateTime.Now);
            sb.AppendLine("Operating System:  " + Environment.OSVersion);
            sb.AppendLine();
            sb.AppendLine("Application:       " + Assembly.GetEntryAssembly().Location);
            sb.AppendLine("Version:           " + Assembly.GetEntryAssembly().GetName().Version);
            sb.AppendLine();
            AppendException(sb, ex);

            textBox1.Text = sb.ToString();
        }

        private void AppendException(StringBuilder sb, Exception ex)
        {
            sb.AppendLine("Exception Source:  " + ex.Source);
            sb.AppendLine("Exception type:    " + ex.GetType());
            sb.AppendLine("Exception message: " + ex.Message);
            sb.AppendLine();
            sb.AppendLine("Stack trace:");
            sb.AppendLine(ex.StackTrace);

            if (ex.InnerException != null)
            {
                sb.AppendLine("--- Inner Exception ---");
                AppendException(sb, ex.InnerException);
            }
        }

        private void detailsButton_Click(object sender, EventArgs e)
        {
            this.Size = new Size(393, 459);
            detailsButton.Enabled = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string url = "http://synomanager.codeplex.com/WorkItem/List.aspx";
            Process.Start(url);
        }
    }
}
